package dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import GsonBean.JcxxChakanBean;
import GsonBean.SuccessBean;
import GsonBean.post.TgysPost;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Ren on 2017/6/29.
 */
//复工预审
public class FgysDialog extends BaseDialog {
    //标题
    TextView title;

    //确认
    TextView confirm;

    //取消
    TextView cancel;

    //同意
    RadioButton ty;

    //不同意
    RadioButton bty;

    //审核意见
    EditText shyj;

    JcxxChakanBean mBean;

    Activity baseActivity;

    public FgysDialog(Context context, JcxxChakanBean mBean, Activity baseActivity) {
        super(context);
        this.mBean=mBean;
        this.baseActivity=baseActivity;
    }

    public FgysDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected FgysDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fgys);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        confirm= (TextView) findViewById(R.id.confirm);
        cancel= (TextView) findViewById(R.id.cancel);
        ty= (RadioButton) findViewById(R.id.ty);
        bty= (RadioButton) findViewById(R.id.bty);
        shyj= (EditText) findViewById(R.id.shyj);
        title= (TextView) findViewById(R.id.title);
    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (bty.isChecked()&&shyj.getText().toString().length()==0) {
                    MyToast.showToast("请输入审核意见");
                    return;
                }
                if (shyj.getText().length()>0&&shyj.getText().toString().trim().length()==0) {
                    MyToast.showToast("请不要输入空格");
                    return;
                }
                    TgysPost mPost=new TgysPost();
                    mPost.setJdjhjcbId(mBean.getRjdjhjcbInfo().getJdjhjcbId());
                    if(ty.isChecked()){
                        //同意
                        mPost.setWhetherAuditAgree("Y");
                    } else {
                        mPost.setWhetherAuditAgree("N");
                    }
                    mPost.setTgshyj(shyj.getText().toString());
                    mPost.setFldw(MyApplication.getmLoginUser().getUserUnitName());
                    mPost.setFlrName(MyApplication.getmLoginUser().getTrueName());
                    mPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());

                    List<TgysPost.LjyyxRjdjhjcbShdw> shdwList=new ArrayList<TgysPost.LjyyxRjdjhjcbShdw>();
                    TgysPost.LjyyxRjdjhjcbShdw shdwBean=new TgysPost.LjyyxRjdjhjcbShdw();
                    shdwBean.setJdjhbBh(mBean.getRjdjhjcbInfo().getRjdjhbBh());
                    shdwBean.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdwBean.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdwBean.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdwBean.setShrname(MyApplication.getmLoginUser().getTrueName());
                    shdwBean.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());

                    shdwList.add(shdwBean);
                    mPost.setJcbShdwList(shdwList);


                    MyApplication.ShowWaitDialog(getContext());
                    NetWorkApi.Fgys(mPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        MyApplication.HideWaitDialog();
                                        SuccessBean successBean = response.body();
                                        if (successBean.getMsg() == null) {
                                            MyToast.showToast("服务器错误");
                                        } else {
                                            if (successBean.getMsg().equals("false")) {
                                                MyToast.showToast("复工预审失败，新检查网络后重新预审");
                                                return;
                                            }
                                            if (successBean.getMsg().equals("success")) {
                                                MyToast.showToast("复工预审成功");
                                                RefreshManager.Jdjc.RefreshJdjclrxx();
                                                RefreshManager.Jdjc.RefreshJdjcdcl();
                                                RefreshManager.Jdjc.RefreshJdjcycl();
                                                getContext().startActivity(new Intent(getContext(), JclrActivity.class));

                                            }
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FgysDialog.this.dismiss();
            }
        });

    }


}
